<?php
//导入头部页面
include_once "header.php";
//include_once "config.inc.php";
//$pdo = new PDO;
//$uid = 0;
$uid = empty($_SESSION['id']) ? 0 : $_SESSION['id'];
//注册
//print_r($_POST);
if(isset($_POST['subreg'])){
	//echo '注册来了....';
	$stmt = $pdo->prepare("select * from kp_user where uname = ?");
	$stmt-> execute(array($_POST['uname']));
	if($stmt->rowCount() > 0){
		//用户名已存在,不能注册,等下处理
		//echo '用户名已存在';
		echo '<script type="text/javascript">alert("用户名已存在!");</script>';
	}else{
		$stmt = $pdo->prepare("insert into kp_user(uname,upwd,email,pwd1,pwd2) values(?,?,?,?,?)");
		//写进数据库,时间问题没做过滤
		$stmt->execute(
		array($_POST['uname']
		,$_POST['upwd']
		,$_POST['email']
		,$_POST['pwd2']
		,$_POST['pwd3'])
		);
		echo '<script type="text/javascript">alert("注册成功!");</script>';
	}
}
//登陆
/*if(isset($_POST['sublogin'])){
	//echo '登陆来了....';
	$ucode = $_SESSION['ucode'];
	if($ucode == $_POST['ucode']){
		$stmt = $pdo->prepare("select id,uname from kp_user where uname = ? and upwd = ?");
		$stmt-> execute(array($_POST['uname'],$_POST['upwd']));
		$_SESSION = $stmt->fetch(PDO::FETCH_ASSOC);
		if(!empty($_SESSION['id'])){
			$uid = $_SESSION['id'];
			echo '<script type="text/javascript">alert("登陆成功!");</script>';
		}else{
			echo '<script type="text/javascript">alert("登陆失败!");</script>';
		}
	}else{
		echo '<script type="text/javascript">alert("验证码不正确!");</script>';
	}
	//print_r($_SESSION);
}*/
//查询出内容列表

//分页==========================
	include "page.class.php";
	$showrow = 5; //一页显示的行数
	$curpage = empty($_GET['page']) ? 1 : $_GET['page']; //当前的页,还应该处理非数字的情况
	$url = "?page={page}"; //分页地址
	//if($uid > 0){
	if(!empty($_GET['uid']) && $_GET['uid'] > 0){
		$sql = "select count(*) from kp_test where uid = ".$uid;
		//$url = "?fl={$uid}&page={page}";
		$url .="&uid=".$uid;
	}else{
		$sql = "select count(*) from kp_test";
	}
	$qsum = $pdo->query($sql);
	$rows = $qsum->fetch();
	$total = $rows[0]; //获取总行数
	if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow))
    $curpage = ceil($total_rows / $showrow); //当前页数大于最后页数，取最后一页
//==分页完显示日志=================

//进行主页数据显示==================
	$listsql = "select * from kp_test where 1=1";
	//echo $uid;
	//if($uid > 0){
	if(!empty($_GET['uid']) && $_GET['uid'] > 0){
		$listsql.=" and uid = {$uid}";
	}
	$listsql .= " order by utime desc,id desc limit ".($curpage-1)*$showrow.",".$showrow;
	//echo $listsql;//sql语句OK
	$stmtlist = $pdo->prepare($listsql);
	//$stmt = $conn->prepare("select * from shop where id limit 0,35");//分页输出开始,数量
	//设置结果集数组模式为关联数组
	$stmtlist->setFetchMode(PDO::FETCH_ASSOC);
	$stmtlist->execute();
	//$arrlist = $stmt->fetchAll();
	//print_r($arrlist);//成功获取到数据
//进行主页数据显示完==================	
	
	//导入模板
include_once "tpl/index.tpl";
?>
